package com.qigao.mall.service.impl.admin;

import com.qigao.mall.api.SysCompanyUserService;
import com.qigao.mall.commons.dto.SysCompanyUserResultDto;
import com.qigao.mall.commons.dto.SysCompanyUserSaveDto;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.jdbc.mapper.SysCompanyMapper;
import com.qigao.mall.jdbc.mapper.SysCompanyUserMapper;
import com.qigao.mall.jdbc.model.SysCompany;
import com.qigao.mall.jdbc.model.SysCompanyUser;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SysCompanyUserServiceImpl implements SysCompanyUserService {
    @Resource
    private SysCompanyUserMapper sysCompanyUserMapper;
    @Resource
    private SysCompanyMapper sysCompanyMapper;

    @Override
    public Result<String> delete(String id) {
         sysCompanyUserMapper.deleteByPrimaryKey(Integer.valueOf(id));
         return Result.newSuccess();
    }

    @Override
    public Result<String> save(SysCompanyUserSaveDto sysCompanyUserSaveDto) {
        SysCompanyUser sysCompanyUser=sysCompanyUserMapper.selectByUniIndex(sysCompanyUserSaveDto.getCompNo(),sysCompanyUserSaveDto.getUserNo(),sysCompanyUserSaveDto.getRoleCode());
        if(sysCompanyUser!=null){
            sysCompanyUser.setModiNo(sysCompanyUserSaveDto.getOperNo());
            sysCompanyUser.setModiTime(new Date());
            sysCompanyUserMapper.updateByPrimaryKeySelective(sysCompanyUser);
        }else {
            sysCompanyUser=new SysCompanyUser();
            BeanUtils.copyProperties(sysCompanyUserSaveDto,sysCompanyUser);
            sysCompanyUser.setCreateNo(sysCompanyUserSaveDto.getOperNo());
            sysCompanyUser.setCreateTime(new Date());
            sysCompanyUserMapper.insertSelective(sysCompanyUser);
        }
        return Result.newSuccess();
    }

    @Override
    public Result<List<SysCompanyUserResultDto>> listByUserAndRoleCode(String userNo, String roleCode) {
        List<SysCompanyUser> sysCompanyUsers=sysCompanyUserMapper.selectByUserNoAndRoleCode(userNo,roleCode);
        if(!CollectionUtils.isEmpty(sysCompanyUsers)){
            List<SysCompanyUserResultDto> sysCompanyUserResultDtos=new ArrayList<>();
            for(SysCompanyUser sysCompanyUser:sysCompanyUsers){
                SysCompanyUserResultDto sysCompanyUserResultDto=new SysCompanyUserResultDto();
                BeanUtils.copyProperties(sysCompanyUser,sysCompanyUserResultDto);
                SysCompany sysCompany= sysCompanyMapper.selectByCompNo(sysCompanyUser.getCompNo());
                if(sysCompany!=null){
                    sysCompanyUserResultDto.setCompName(sysCompany.getCompName());
                }
                sysCompanyUserResultDtos.add(sysCompanyUserResultDto);
            }
            return Result.newSuccess(sysCompanyUserResultDtos);
        }
        return Result.newSuccess();
    }
}
